import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import 'element-plus/dist/index.css'
import 'normalize.css/normalize.css'
import './styles/index.scss'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import './permission'
// 引入自定义指令
import ClipboardDirective from './directive/clipboard'
import DragDialogDirective from './directive/el-drag-dialog'
import ElTableDirective from './directive/el-table'
import PermissionDirective from './directive/permission'
import WavesDirective from './directive/waves'
// 引入错误日志
import { setupErrorLog } from './utils/error-log'
// 引入 SvgIcon 组件
import SvgIcon from '@/components/SvgIcon/index.vue'
// 导入 SVG 图标
import 'virtual:svg-icons-register'

export const app = createApp(App)

// 注册 SvgIcon 组件
app.component('svg-icon', SvgIcon)

// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 注册所有自定义指令
app.use(ClipboardDirective)
app.use(DragDialogDirective)
app.use(ElTableDirective)
app.use(PermissionDirective)
app.use(WavesDirective)

// 设置错误处理
setupErrorLog(app)

// 全局属性替代过滤器
import * as filters from './filters'
app.config.globalProperties.$filters = filters

// 注册 Element Plus
app.use(ElementPlus, {
  locale: zhCn,
  size: localStorage.getItem('size') || 'default'
})

// 添加全局消息提示
app.config.globalProperties.$message = ElementPlus.ElMessage

// 注册 Pinia
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
app.use(pinia)

// 注册路由
app.use(router)

app.mount('#app')